package com.custom.customsweather

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * @description：TODO
 * @Author MRyan
 * @Date 2020/3/24 20:42
 * @Version 1.0
 */

class CustomClock @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    // 自定义View宽度
    var weatherViewWidth: Int = 0

    // 自定义View高度
    var weatherViewHeight: Int = 0

    var centerX: Float = 0f
    var centerY: Float = 0f

    // 内圆半径
    val innerRadius = dp2px(80f)

    // 外圆半径
    val outRadius = dp2px(90f)

    // 阴影半径
    val shadowRadius = dp2px(30f)


    init {
        // 禁用硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null)

    }

    private val outPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#e6e8db")
        maskFilter = BlurMaskFilter(shadowRadius.toFloat(), BlurMaskFilter.Blur.SOLID)
    }

    // 内圆paint
    private val innerCirclePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        shader = LinearGradient(
            centerX - innerRadius, centerY + innerRadius, centerX, centerY - innerRadius,
            Color.parseColor("#e0e2e5"), Color.parseColor("#758595"), Shader.TileMode.CLAMP
        )
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        weatherViewWidth = MeasureSpec.getSize(widthMeasureSpec)
        weatherViewHeight = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(weatherViewWidth, weatherViewHeight)
        // 圆心横坐标
        centerX = weatherViewWidth / 2f
        // 圆心纵坐标
        centerY = weatherViewHeight / 2f
    }


    override fun onDraw(canvas: Canvas?) {
        if (canvas != null) {
            canvas.drawCircle(centerX, centerY, outRadius.toFloat(), outPaint)
            canvas.drawCircle(centerX, centerY, innerRadius.toFloat(), innerCirclePaint)
            canvas.save()
        }
    }

    fun dp2px(dpValue: Float): Int {
        val scale = this.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }
}